<template>
    <div>
        <el-card style="max-width: 100%">
            <el-form
              ref="ruleFormRef"
              style="max-width: 600px;margin: auto ;"
              :model="ruleForm"
              :rules="rules"
              label-width="auto"
            >
              <el-form-item label="物料编码" prop="MaterialCode">
                <el-input v-model="ruleForm.MaterialCode" />
              </el-form-item>
              
              <el-form-item label="供应商编码" prop="SuppilyId">
                <el-input v-model="ruleForm.SuppilyId" />
              </el-form-item>
              
              <el-form-item label="批次号" prop="BatchCode">
                <el-input v-model="ruleForm.BatchCode" />
              </el-form-item>
              
              
              <el-form-item>
                <el-button type="primary" @click="submitLockForm(ruleFormRef)">
                  锁定
                </el-button>
                
                
                <el-button type="primary" @click="submitUnlockForm(ruleFormRef)">
                  解锁
                </el-button>
                

                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
              </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios'



const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  MaterialCode: '',
  SuppilyId: 0,
  BatchCode: '',
  IsLock:0
})

const rules = reactive<FormRules>({
  MaterialCode: [
    { required: true, message: '请输入人物料编码！', trigger: 'blur' },
  ],
  
})

const submitLockForm = async (formEl: FormInstance | undefined) => {

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(ruleForm)
      ruleForm.IsLock = 1;
      axios.get('http://localhost:9991/api/UpdateRedIsLock/UpdateRedIsLock',{params:{MaterialCode:ruleForm.MaterialCode,SuppilyId:ruleForm.SuppilyId,BatchCode:ruleForm.BatchCode,IsLock:ruleForm.IsLock}}).then(res=>{
        if(res.data != 0){
          ElMessage.success("锁定成功！");
          ruleForm.BatchCode = '',
          ruleForm.MaterialCode = '',
          ruleForm.SuppilyId = 0
        }
        else{
          ElMessage.error("物料不存在！锁定失败！");
        }
      });
    } else {
      console.log('error submit!', fields)
    }
  })
}
const submitUnlockForm = async (formEl: FormInstance | undefined) => {

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(ruleForm)
      ruleForm.IsLock = 0;
      axios.get('http://localhost:9991/api/UpdateRedIsLock/UpdateRedIsLock',{params:{MaterialCode:ruleForm.MaterialCode,SuppilyId:ruleForm.SuppilyId,BatchCode:ruleForm.BatchCode,IsLock:ruleForm.IsLock}}).then(res=>{
        if(res.data != 0){
          ElMessage.success("解锁成功！");
          ruleForm.BatchCode = '',
          ruleForm.MaterialCode = '',
          ruleForm.SuppilyId = 0
        }
        else{
          ElMessage.error("物料不存在！解锁失败！");
        }
      });
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>

<style scoped>

</style>